const util = require('../../util.js')

const app = getApp()
const log = t => getApp().globalData.logger.log('[entry]' + t)

let secretTouched = 0
let secretTimeout = null

Page({
  data: {
    cloudEnv: ''
  },

  onLoad(options) {
    this.showAlertIfEnvNotSupported()
    util.readAppParam(options)
    if (app.globalData.logger.getLogLevel() > 0) {
      this.setData({
        cloudEnv: app.globalData.debugSetting.cloudEnv
      })
    }
  },

  onReady() {
    // [Workaround] If we don't have storage access permission, force to trigger the permission dialog
    my.downloadFile({
      url: 'https://app-master.youcamapi.cn/miniprogram/App-Taobao/Makeup/makeup-sdk/triggerStoragePermissionImg.png'
    })
  },

  onHide() {
    this.removeSecretTimeout()
  },

  eyelashFitting: function() {
    log('openCameraPage')
    my.navigateTo({
      url: '/pages/tryOn/tryOn'
    })
  },

  onSecretStart(e) {
    secretTouched += 1
    if (secretTouched === 2 && !secretTimeout) {
      my.vibrateShort()
      secretTimeout = setTimeout(() => {
        secretTouched = 0
        secretTimeout = null
        my.showActionSheet({
          items: ["祕技頁", "Logcat", "上傳小工具"],
          success: result => {
            switch (result.index) {
              case 0:
                log("open secretPage");
                my.navigateTo({
                  url: "/pages/secretPage/secretPage"
                });
                break;
              case 1:
                log("open logPage");
                my.navigateTo({
                  url: "/pages/logPage/logPage"
                });
                break;
              case 2:
                log("open uploadToolPage");
                my.navigateTo({
                  url: "/pages/uploadToolPage/uploadToolPage"
                });
                break;
            }
          }
        })
      }, 3000)
    }
  },

  onSecretEnd(e) {
    secretTouched = 0
    this.removeSecretTimeout()
  },

  removeSecretTimeout() {
    if (secretTimeout) {
      clearTimeout(secretTimeout)
      secretTimeout = null
    }
  },


  showAlertIfEnvNotSupported() {
    const res = this.checkEnv()
    if (!res.supported) {
      my.alert({
        content: res.msg,
        success: () => my.exit && my.exit()
      })
    }
  },

  checkEnv() {
    if (!util.isSupportedAppVersion()) {
      return {
        supported: false,
        msg: '亲，您的淘宝版本也太旧啦，功能无法展现，赶紧去更新一下吧'
      }
    }

    return {
      supported: true,
      msg: ''
    }
  }
})

